/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.admin.controller.CreateEventEJBRemote;
import com.admin.controller.ShowEventEJBRemote;
import com.admin.controller.UpdateEventEJBRemote;
import com.common.json.EventLibraryJson;
import com.common.json.EventManagerJson;
import com.entities.EventInfo;
import com.entities.EventLibrary;
import com.entities.EventRegistration;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Tuan Long
 */
public class EventLibraryModel {

    CreateEventEJBRemote createEventEJB;
    UpdateEventEJBRemote updateEventEJB;
    ShowEventEJBRemote showEventEJB;

    public EventLibraryModel() {
        createEventEJB = lookupCreateEventEJBRemote();
        updateEventEJB = lookupUpdateEventEJBRemote();
        showEventEJB = lookupShowEventEJBRemote();
    }

    private CreateEventEJBRemote lookupCreateEventEJBRemote() {
        try {
            Context c = new InitialContext();
            return (CreateEventEJBRemote) c.lookup("java:global/BluePumpkinEJB/CreateEventEJB!com.admin.controller.CreateEventEJBRemote");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private UpdateEventEJBRemote lookupUpdateEventEJBRemote() {
        try {
            Context c = new InitialContext();
            return (UpdateEventEJBRemote) c.lookup("java:global/BluePumpkinEJB/UpdateEventEJB!com.admin.controller.UpdateEventEJBRemote");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private ShowEventEJBRemote lookupShowEventEJBRemote() {
        try {
            Context c = new InitialContext();
            return (ShowEventEJBRemote) c.lookup("java:global/BluePumpkinEJB/ShowEventEJB!com.admin.controller.ShowEventEJBRemote");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    /**
     * @EventLibraryModel.createNewEvent: insert new record into EventLibrary
     * table
     * @param jsonstr
     * @return boolean
     */
    public boolean createNewEvent(String jsonstr) {
        return createEventEJB.createEvent(jsonstr);
    }

    /**
     * @EventLibraryModel.updateEvent: insert new record into EventLibrary table
     * @param jsonstr
     * @return boolean
     */
    public boolean updateEvent(String jsonstr) {
        return updateEventEJB.updateEvent(jsonstr);
    }

    /**
     * @EventLibraryModel.fillEventsDataTable : fill information ad
     * EventsDataTable at view: events.xhtml
     * @return List<>
     */
    public List<EventLibrary> fillEventsDataTable() {
        List<EventLibrary> list = new ArrayList<EventLibrary>();
        List<String> listJson = showEventEJB.fillEventsDataTable();
        for (String json : listJson) {
            EventLibrary el = EventLibraryJson.convertJSONObjectStringToEventLibraryForFillEventsDataTable(json);
            list.add(el);
        }
        return list;
    }

    public List<EventRegistration> fillRegisteredTable() {
        List<EventRegistration> list = new ArrayList<EventRegistration>();
        List<String> listJson = showEventEJB.fillRegisteredTable();
        for (String json : listJson) {
            EventRegistration er = EventManagerJson.convertJsonStringToEventRegistrationObjectForFillRegisteredTable(json);
            list.add(er);
        }
        return list;
    }

    public List<EventRegistration> fillTotalRegistered() {
        List<EventRegistration> list = new ArrayList<EventRegistration>();
        List<String> listJson = showEventEJB.fillTotalRegistered();
        if (listJson != null) {
            for (String json : listJson) {
                EventRegistration er = EventManagerJson.convertJsonStringToEventRegistrationObjectForFillTotalRegistered(json);
                list.add(er);
            }
        }
        return list;
    }

    public List<EventLibrary> fillTotalEventTypeTable() {
        List<EventLibrary> list = new ArrayList<EventLibrary>();
        List<String> listJson = showEventEJB.fillTotalEventTypeTable();
        if (listJson != null) {
            for (String json : listJson) {
                EventLibrary el = EventManagerJson.convertJsonStringToEventRegistrationObjectForFillTotalEventTypeTable(json);
                list.add(el);
            }
        }
        return list;

    }

    /**
     * EventLibraryModel.getEventContentById return EventLibrary object DESC:
     * find EventLibrary in database by eventID
     *
     * @param eventId String
     * @return EventLibrary
     */
    public EventLibrary getEventContentById(String eventId) {
        EventLibrary el = new EventLibrary();
        try {
            int id = Integer.parseInt(eventId);
            String json = showEventEJB.getEventContentById(id);
            if (json != null) {
                el = EventManagerJson.convertJsonStringToEventLibraryForGetEventContentById(json);
            }
        } catch (NumberFormatException e) {
            return el;
        }
        return el;
    }

    public EventInfo getEventContentById2(String eventId) {
        EventInfo ei = new EventInfo();
        try {
            int id = Integer.parseInt(eventId);
            String json = createEventEJB.getEventContentById2(id);
            System.out.println("EventInfo: " + json);
            if (json != null) {
                ei = EventManagerJson.convertJsonStringToEventInfoForGetEventContentById2(json);
            }
        } catch (NumberFormatException e) {
            return ei;
        }
        return ei;
    }

    public EventLibrary getEventUpdateContentById(int id) {
        EventLibrary el = new EventLibrary();
        String json = showEventEJB.getUpdateEventInformationById(id);
        if (json != null) {
            el = EventLibraryJson.convertJSONObjectStringToEventLibrary(json);
        }
        return el;
    }

    public List<EventLibrary> getTop10EventIdAndNameByEventTag(String eventType) {
        List<EventLibrary> list = new ArrayList<EventLibrary>();
        List<String> listJson = new ArrayList<String>();
        if (eventType != null) {
            listJson = showEventEJB.getTop10EventIdAndNameByEventTag(eventType);
            for (String json : listJson) {
                EventLibrary el = EventManagerJson.convertJsonStringToEventLibraryForGetTop10EventIdAndNameByEventTag(json);
                list.add(el);
            }
        }
        return list;

    }

    public List<EventLibrary> getTop10EventIdAndName() {
        List<EventLibrary> list = new ArrayList<EventLibrary>();
        List<String> listJson = new ArrayList<String>();
        listJson = showEventEJB.getTop10EventIdAndName();
        for (String json : listJson) {
            EventLibrary el = EventManagerJson.convertJsonStringToEventLibraryForGetTop10EventIdAndNameByEventTag(json);
            list.add(el);
        }
        return list;
    }

    public List<EventLibrary> getEventComming(int day) {
        List<EventLibrary> list = new ArrayList<EventLibrary>();
        List<String> listJson = showEventEJB.getEventComming(day);
        if (listJson != null) {
            for (String json : listJson) {
                EventLibrary el = EventManagerJson.convertJsonEventCommingDataToEventLibrary(json);
                list.add(el);
            }
        }
        return list;
    }

}
